// page_two/xdxd/xdxd.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,

  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title:"设备报备",
    photo: [],
    cltimeguo:'',
    chuguo:'',
    chuid:'',
    wuphoto:'',
    bindsgr:'',
    cltimeguo: '请选择时间', //处理时间
    leiguo: '请选择类别', //废弃油脂类别
    leiid:'',
    sdxdsl: '1', //洗涤消毒数量
    sdxdmc:"",
    sdxdcs:"",
    bindsgrdian:'',
    tongyi:'',

  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  agreement(){
    wx.navigateTo({
      url: '/page_two/shuoming/shuoming?cid=284'
    })
  },
  sdxdmc(e) {
    this.setData({
      sdxdmc: e.detail.value
    })
  },
  sdxdcs(e) {
    this.setData({
      sdxdcs: e.detail.value
    })
  },
  // 数量
  shuliang(e) {
    console.log(e)
    var lll = e.detail.value
    this.setData({
      sdxdsl: lll
    })
    if (lll.length >= 4) {
      wx.showToast({
        title: '只能输入四位哦~',
        icon: 'none'
      })
    }
  },
  // 减
  jian() {
    var num = this.data.sdxdsl
    if (num == 1) {
      this.setData({
        sdxdsl: 1
      })
    } else {
      num--
      this.setData({
        sdxdsl: num
      })
    }
  },
  // 加
  jia() {
    var num = this.data.sdxdsl
    num++
    this.setData({
      sdxdsl: num
    })
  },
  bindcltime(e) {
    if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
        wx.showToast({
          title: '有效期不可小于当前时间',
          icon: 'none'
        })
        this.setData({
            cltimeguo: '请选择时间',
        })
      }else{
          this.setData({
            cltimeguo: e.detail.value
          })
      } 
  },
  bindlei(e) {
    var index = e.detail.value
    this.setData({
      leiguo: this.data.leilist[index].name,
      leiid: this.data.leilist[index].id
    })
  },
  bindchu(e) {
    let index = e.detail.value
    this.setData({
      chuguo: this.data.chulist[index].Name,
      chuid: this.data.chulist[index].id
    })
  },
  bindsgrdian(e) {
    this.setData({
      bindsgrdian: e.detail.value
    })
  },
  lanmufl() {
    let that = this
    let type_mold = '';
      type_mold = 50;
    

    this.myRequest(yuming + 'Besall/public_class_options', {
      type_mold:type_mold,
    }, "POST").then(res => {
      
      
        that.setData({
          leilist: res.data.data
        })
      
   
    })
  },
  cylist() {
    var that = this
    this.myRequest(yuming + 'shop/shopSelectStaff', {
      page: -1,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      that.setData({
        chulist: res.data.result,
      })
    })
  },
   // 上传
   chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
   // 收购人
   bindsgr(e) {
    this.setData({
      bindsgr: e.detail.value
    })
  },
  checkboxChange(e){
    console.log(e);
    let tongyi = e.detail.value
    this.setData({
        tongyi:tongyi
    })
  },
  tijiao(){
    if(this.data.leiid == ''){
      wx.showToast({
        title: '请选择类别',
        icon:'none'

      })
      return
    }
    if(this.data.bindsgrdian == ''){
      wx.showToast({
        title: '请输入名称',
        icon:'none'
      })
      return
    }
    if(this.data.sdxdmc == ''){
      wx.showToast({
        title: '请输入厂商',
        icon:'none'

      })
      return
    }
    if(this.data.sdxdcs == ''){
      wx.showToast({
        title: '请输入品牌',
        icon:'none'

      })
      return
    }
    if(this.data.cltimeguo == '请选择时间'){
      wx.showToast({
        title: '请选择时间',
        icon:'none'

      })
      return
    }
    if(this.data.wuphoto == ''){
      wx.showToast({
        title: '请上传图片',
        icon:'none'

      })
      return
    }
  //   if(this.data.tongyi != 'true'){
  //     wx.showToast({
  //       title: '请阅读上报说明',
  //       icon:'error'
  //     })
  //     return
  // }

    this.myRequest(yuming + 'Shopthat/clean_device_add',{
      shopId:wx.getStorageSync('shopId'),
      classId:this.data.leiid,
      name:this.data.bindsgrdian,
      number:this.data.sdxdsl,
      manufacturer:this.data.sdxdmc,
      cleanBrand:this.data.sdxdcs,
      validityTime:this.data.cltimeguo,
      cleanImage:this.data.wuphoto
    },'POST').then(res=>{
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
        }, 1000)
      } 
    })
  },
   // 放大
   fangda(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      // title: options.title,
      // id: options.id,
      // cid: options.cid
    })
    this.lanmufl()
    this. cylist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})